import type { OrderCreateParams, OrderCreateResult, OrderListParams, OrderUpdataParams, OrderListResult, OrderLogisticResult, OrderPreResult, OrderResult } from "@/types/order"
import { http as https } from "@/utils/https"
import { http } from "@/utils/http"
import type { CartItem } from "@/types/cart"

//填写订单信息
export const getMemberOrderPreAPI = (id: string) => {
  return https<CartItem[]>({
    method: "GET",
    url: `/cart/getSelectd?id=${id}`,
  })
}

/**
 *
 * @param data 请求体参数 skuId sku的id count 数量 addressId 地址的id(可选)
 * @returns
 */

export const getMemberOrderPreNowAPI = (data: {
  skuId: string,
  count: string,
  addressId?: string
}) => {
  return http<OrderPreResult>({
    method: "GET",
    url: '/member/order/pre/now',
    data,
  })
}

/**
 *
 * @param data 请求体参数
 * @returns
 */
export const postMemberOrderAPI = (data: OrderCreateParams) => {
  const res = https({
    method: 'POST',
    url: '/order',
    data
  })
  return res
}

/**
 *
 * @param data 请求体参数
 * @returns
 */
export const getMemberOrderByIdAPI = (id: string) => {
  return https<OrderResult[]>({
    method: 'GET',
    url: `/order/id?id=${id}`,
  })
}

/**
 *
 * @param orderId 订单id
 * @returns
 */
export const ReGetOrderByOrderId = (orderId: string) => {
  return https<OrderResult[]>({
    method: 'GET',
    url: `/order/regetOrder?orderId=${orderId}`
  })
}

/**
 * 模拟发货-内测版
 * @description 在DEV环境下使用，仅在订单状态为待发货时，可模拟发货，调用后订单状态修改为待收货，包含模拟物流。
 * @param id 订单id
 */
export const getMemberOrderConsignmentByIdAPI = (id: string) => {
  return http({
    method: 'GET',
    url: `/member/order/consignment/${id}`,
  })
}

/**
 * 确认收货
 * @description 仅在订单状态为待收货时，可确认收货。
 * @param id 订单id
 */
export const putMemberOrderReceiptByIdAPI = (id: string) => {
  return http<OrderResult>({
    method: 'PUT',
    url: `/member/order/${id}/receipt`,
  })
}

/**
 * 获取订单物流
 * @description 仅在订单状态为待收货，待评价，已完成时，可获取物流信息。
 * @param id 订单id
 */
export const getMemberOrderLogisticsByIdAPI = (id: string) => {
  return http<OrderLogisticResult>({
    method: 'GET',
    url: `/member/order/${id}/logistics`,
  })
}

/**
 * 删除订单
 * @description 仅在订单状态为待评价，已完成，已取消时，可删除订单。
 * @param data ids 订单集合
 */
export const deleteMemberOrderAPI = (orderId: string) => {
  return http({
    method: 'DELETE',
    url: `/order`,
  })
}
/**
 * 取消订单
 * @description 仅在订单状态为待付款时，可取消订单。
 * @param id 订单id
 * @param data cancelReason 取消理由
 */
export const getMemberOrderCancelByIdAPI = (id: string,) => {
  return https({
    method: 'GET',
    url: `/order/cancel?orderId=${id}`,
  })
}

/**
 * 获取订单列表
 * @param id 用户Id */
export const getMemberOrderAPI = (data: OrderListParams) => {
  return https<OrderResult[]>({
    method: 'POST',
    url: "/order/userId",
    data: data
  })
}

export const updateOrder = (order_Id: string, data: OrderUpdataParams) => {
  return https<OrderResult[]>({
    method: "POST",
    url: `/order/update?order_Id=${order_Id}`,
    data: data
  })
}

/**
 * 支付成功 修改订单状态
 */
export const changeOrderState = (order_no: string) => {
  return https({
    method: 'GET',
    url: `/order/change?order_no=${order_no}`
  })
}
